package com.b1502.store2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.Category;

/**
 * Created by zhangdongxu on 2017/2/27.
 */

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> implements View.OnClickListener,View.OnLongClickListener{

    private addOncli mOncli = null;
    private Context context;
    private Category[] categories;
    @Override
    public boolean onLongClick(View view) {
        mOncli.onLongClick(view, (Category) view.getTag());
        return false;
    }


    //define interface
    public interface addOncli {
        void onItemClick(View view , Category data);
        void onLongClick(View view , Category data);
    }

    public CategoryAdapter(Context context, Category[] categories) {
        this.context = context;
        this.categories = categories;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.category_item,null);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(categories[position].getName());
        holder.itemView.setTag(categories[position]);
    }

    @Override
    public int getItemCount() {
        return categories.length;
    }

    @Override
    public void onClick(View view) {
        mOncli.onItemClick(view, (Category) view.getTag());
    }
    public void setOnclick(addOncli listener){
        this.mOncli = listener;
    }
    public void setLoingOnclick(addOncli lister){
        this.mOncli =lister;
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv=(TextView) itemView.findViewById(R.id.fl_tv);
        }
    }


}

